package br.com.android.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements OnPageChangeListener {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	private final String CURRENT_TAB_KEY = "CURRENT_TAB";

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_main );

		mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager() );

		mViewPager = (ViewPager) findViewById( R.id.pager );
		mViewPager.setAdapter( mSectionsPagerAdapter );

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Toast.makeText( getApplicationContext(), "PAUSE " + getFragmentManager().findFragmentById( R.layout.activity_fragment2 ).getView(), Toast.LENGTH_SHORT ).show();

	}

	@Override
	protected void onSaveInstanceState( Bundle outState ) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState( outState );
		outState.putInt( CURRENT_TAB_KEY, mViewPager.getCurrentItem() );
		Toast.makeText( getApplicationContext(), "CURRENT " + mViewPager.getCurrentItem(), Toast.LENGTH_SHORT ).show();

	}

	@Override
	protected void onRestoreInstanceState( Bundle savedInstanceState ) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState( savedInstanceState );
		mViewPager.setCurrentItem( savedInstanceState.getInt( CURRENT_TAB_KEY ) + 1 );
	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu ) {
		getMenuInflater().inflate( R.menu.activity_main, menu );
		return true;
	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		private Fragment2 fragment = new Fragment2();

		public SectionsPagerAdapter( FragmentManager fm ) {
			super( fm );
		}

		@Override
		public Fragment getItem( int position ) {
			fragment = new Fragment2();
			Bundle args = new Bundle();
			args.putInt( Fragment2.ARG_SECTION_NUMBER, position );
			fragment.setArguments( args );
			return fragment;
		}

		public Fragment2 getFragment() {
			return fragment;
		}

		@Override
		public int getCount() {
			return fragment.getSize();
		}

		@Override
		public CharSequence getPageTitle( int position ) {
			return "Pergunta " + ( position + 1 );
		}

		@Override
		public String toString() {
			return "SectionsPagerAdapter [fragment=" + fragment.getView() + "]";
		}

	}

	@Override
	public void onPageScrollStateChanged( int arg0 ) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled( int arg0, float arg1, int arg2 ) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected( int arg0 ) {
		// TODO Auto-generated method stub
		Toast.makeText( getApplicationContext(), "CURRENT " + mViewPager.getCurrentItem(), Toast.LENGTH_SHORT ).show();

	}

}
